import numpy as np
from numpy import linalg as LA
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
from keras import backend as k

conf = k.tf.ConfigProto(device_count={'CPU': 1},
                        intra_op_parallelism_threads=1,
                        inter_op_parallelism_threads=1)
k.set_session(k.tf.Session(config=conf))


'''
Use vgg16 model to extract features
Output normalized feature vector
'''


def extract_feat(img_path):
    k.clear_session()
    model_1 = VGG16(weights='imagenet', input_shape=(224,224, 3),
                   pooling='max', include_top=False)
    img = image.load_img(img_path, target_size=(224, 224))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    feat = model_1.predict(img)
    norm_feat = feat[0]/LA.norm(feat[0])
    return norm_feat





